package top.hidoctor.auscultation.feature.activity;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.tang.user.UserInfoManager;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import top.hidoctor.auscultation.R;
import top.hidoctor.auscultation.base.BaseActivity;
import top.hidoctor.auscultation.bean.UserBean;
import top.hidoctor.auscultation.bean.YangbenBean;
import top.hidoctor.auscultation.event.AuditZdEvent;
import top.hidoctor.auscultation.event.LabelZdEvent;
import top.hidoctor.auscultation.net.c.SendPackageContract;
import top.hidoctor.auscultation.net.req.UserReq;
import top.hidoctor.auscultation.net.p.SendPackagePresenter;
import top.hidoctor.auscultation.net.req.AuditZdReq;
import top.hidoctor.auscultation.net.req.LabelZdReq;
import top.hidoctor.auscultation.net.req.SimleReq;

public class SendPackageActivity extends BaseActivity implements View.OnClickListener, SendPackageContract.View {
    private EditText editYangben, editDay, editRenyuan, editChoucha;
    private TextView tvYangben, tvRenyuan, tvYangbenGuizhe, tvRenyuanGuizhe, tvType;

    private OptionsPickerView optionsPicker;
    private SendPackagePresenter presenter;
    private SimleReq simleReq;
    private UserReq userReq;
    private LabelZdReq labelZdReq;
    private Button btnConfirm;
    private Integer maxYb;
    private Integer maxRy;
    private String type;
    private RelativeLayout rlType, rlDay, rlChoucha, rlPeople;
    private AuditZdReq auditZdReq;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_send_package);
        type = getIntent().getStringExtra("type");//1 标注包 2 审核包
        initEvent();
        initView();
    }

    private void initEvent() {

        presenter = new SendPackagePresenter(this, this);
        userReq = new UserReq();
        if (type.equals("1")) {
            simleReq = new SimleReq("0", "20", 1);

            labelZdReq = new LabelZdReq();
            userReq.setIsDeleted(1);
            userReq.setStatus("2");
            userReq.setRoleIds("[\"3124297098854400\"]");
            userReq.setScope(2);
            presenter.getYbList(simleReq);
            presenter.getUserList(userReq);
        } else {
            maxRy = 1;
            auditZdReq = new AuditZdReq();
            userReq.setRoleIds("[\"3124297241460736\"]");
            userReq.setIsDeleted(1);
            userReq.setStatus("2");
            userReq.setScope(2);
            presenter.getUserList(userReq);
            presenter.getDifficult();
        }
    }

    private void initView() {
        Toolbar toolbar = findViewById(R.id.toolbar);
        TextView tvTitle = findViewById(R.id.tv_title);
        tvTitle.setText("自动派包");
        toolbar.setNavigationIcon(R.mipmap.ic_back_left);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        editYangben = findViewById(R.id.edit_yangben); //样本数量最多30个
        editRenyuan = findViewById(R.id.edit_renyuan); //人员数量最少2个
        tvYangben = findViewById(R.id.tv_yangben);
        btnConfirm = findViewById(R.id.btn_confirm);
        tvRenyuan = findViewById(R.id.tv_renyuan);
        editDay = findViewById(R.id.edit_day);//最少7天
        tvYangbenGuizhe = findViewById(R.id.tv_yangben_guizhe);
        tvRenyuanGuizhe = findViewById(R.id.tv_renyuan_guizhe);
        rlType = findViewById(R.id.rl_type);
        tvType = findViewById(R.id.tv_type);
        rlPeople = findViewById(R.id.rl_people);
        rlDay = findViewById(R.id.rl_day);
        rlChoucha = findViewById(R.id.rl_choucha);
        editChoucha = findViewById(R.id.edit_choucha);


        tvYangbenGuizhe.setOnClickListener(this);
        btnConfirm.setOnClickListener(this);
        tvType.setOnClickListener(this);

        tvType.setText("疑难");

        if (type.equals("1")) {//标注
            tvYangben.setText("可标注样本：0");
            tvRenyuan.setText("可标注人员：0");
            rlPeople.setVisibility(View.VISIBLE);
            rlType.setVisibility(View.GONE);
            rlDay.setVisibility(View.VISIBLE);
        } else {//审核
            tvYangben.setText("可审核样本：0");
            tvRenyuan.setText("可审核人员：0");
            rlPeople.setVisibility(View.GONE);
            rlType.setVisibility(View.VISIBLE);
            rlDay.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_yangben_guizhe:
                hideSoft(tvYangbenGuizhe);
                showPicker(1);
                break;
            case R.id.tv_type:
                hideSoft(tvType);
                showPicker(2);
                break;
            case R.id.btn_confirm:
                String type = tvType.getText().toString();
                String yangben = editYangben.getText().toString();
                String renyuan = editRenyuan.getText().toString();
                String day = editDay.getText().toString();
                String guizhe = tvYangbenGuizhe.getText().toString();
                String choucha = editChoucha.getText().toString();
                if (this.type.equals("2")) {
                    if (TextUtils.isEmpty(type)) {
                        showToast("请选择派包类型");
                        return;
                    }
                }
                if (TextUtils.isEmpty(yangben)) {
                    showToast("请输入样本数量");
                    return;
                }
                if (Integer.valueOf(yangben) > maxYb) {
                    showToast("最大样本数量为" + maxYb);
                    return;
                }
                if (Integer.valueOf(yangben) > 30) {
                    showToast("最大样本数量为30");
                    return;
                }

                if (this.type.equals("1")) {

                    if (TextUtils.isEmpty(day)) {
                        showToast("请输入有效天数");
                        return;
                    }
                }
                if (this.type.equals("2")) {
                    if (rlChoucha.getVisibility() == View.VISIBLE) {
                        if (TextUtils.isEmpty(choucha)) {
                            showToast("请输入抽查数量");
                            return;
                        }
                    }
                }

                if (TextUtils.isEmpty(guizhe)) {
                    showToast("请选择样本规则");
                    return;
                }
                if (this.type.equals("1")) {
                    labelZdReq.setHospitalId(UserInfoManager.getInstance().getUserInfo().getHospitalId());
                    labelZdReq.setSampleTotal(Integer.valueOf(yangben));
                    labelZdReq.setMarkEmpTotal(Integer.valueOf(renyuan));
                    labelZdReq.setExpiryDay(Integer.valueOf(day));
                    labelZdReq.setSampleRule(guizhe.equals("随机") ? 10 : guizhe.equals("正序") ? 20 : guizhe.equals("倒序") ? 30 : -1);
                    presenter.addLabel(labelZdReq);
                } else {
                    auditZdReq.setHospitalId(UserInfoManager.getInstance().getUserInfo().getHospitalId());
                    auditZdReq.setType(guizhe.equals("待审核") ? 10 : 20);
                    if (rlChoucha.getVisibility() == View.VISIBLE) {
                        auditZdReq.setCheckNum(Integer.valueOf(choucha));
                    }
                    auditZdReq.setSampleTotal(Integer.valueOf(yangben));
                    auditZdReq.setSampleRule(guizhe.equals("随机") ? 10 : guizhe.equals("正序") ? 20 : guizhe.equals("倒序") ? 30 : -1);
                    presenter.addAudit(auditZdReq);
                }

                break;
        }
    }

    /**
     *显示选择框
     * @param type
     */
    private void showPicker(final int type) {
        final ArrayList<String> list = new ArrayList<>();
        if (type == 1) {
            list.add("正序");
            list.add("倒序");
            list.add("随机");
        } else {
            list.add("疑难");
//            list.add("待审核");
        }

        //设置选中项文字颜色
        optionsPicker = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                if (type == 1) {
                    tvYangbenGuizhe.setText(list.get(options1));
                } else {
                    if (list.get(options1).equals("待审核")) {
                        rlChoucha.setVisibility(View.VISIBLE);
                    } else {
                        rlChoucha.setVisibility(View.GONE);
                    }
                    tvType.setText(list.get(options1));
                }
            }
        })
                .setTitleText(type == 1 ? "样本规则" : "派包类型")
                .setDividerColor(ContextCompat.getColor(SendPackageActivity.this, R.color.blue_main))
                .setTitleSize(18)
                .setContentTextSize(15)
                .setOutSideCancelable(true)
                .setSubmitColor(ContextCompat.getColor(SendPackageActivity.this, R.color.blue_main))
                .setCancelColor(ContextCompat.getColor(SendPackageActivity.this, R.color.colorHintText))
                .setTextColorCenter(ContextCompat.getColor(SendPackageActivity.this, R.color.blue_main)) //设置选中项文字颜色
                .build();
        optionsPicker.setPicker(list);
        optionsPicker.show();
    }


    /**
     *获取可标注样本数量
     * @param number
     */
    @Override
    public void getYbListSuc(String number) {
        maxYb = Integer.valueOf(number);
        tvYangben.setText("可标注样本：" + number);
    }

    /**
     * 获取标注审核人员数量
     * @param bean
     */
    @Override
    public void getUserListSuc(UserBean bean) {
        maxRy = bean.getTotalItems();
        tvRenyuan.setText((type.equals("1") ? "可标注人员：" : "可审核人员：") + bean.getTotalItems());

    }

    /**
     * 自动派包成
     */
    @Override
    public void addLabelSuc() {
        EventBus.getDefault().post(new LabelZdEvent());
        showToast("自动派包成功！");
        finish();
    }

    /**
     * 自动派包成功
     */
    @Override
    public void addAuditSuc() {
        EventBus.getDefault().post(new AuditZdEvent());
        showToast("自动派包成功！");
        finish();
    }

    /**
     * 获取可审核样本数量
     * @param bean
     */
    @Override
    public void getDifficultSuc(YangbenBean bean) {
        maxYb = Integer.valueOf(bean.getTotalItems());
        tvYangben.setText("可审核样本：" + bean.getTotalItems());
    }
}
